<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>index</title>
    <link href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <style>
        .before-input {
            float: left;
            width: 60px;
        }
    </style>
</head>

<body>
    <div id="root">
        <div id="header">
            <h1>欢迎来到BAO</h1>
            <p>Blade art online这是一个追求至简的多人在线MMORPG游戏</p>
            <p>请选择你要进行的操作<i class="fa fa-hand-o-down"></i></p>
        </div>
        <hr/>

        <div id="body">

            <div id="login_form">
                <h3>登录</h3>
                <span class="before-input">用户名:</span> <input name="loginName" /> <br/>
                <span class="before-input">密 码:</span> <input type="password" name="loginPass" /> <br/>
                <button onclick="login()">登录</button>
            </div>

            <br/>

            <div id="register_form">
                <h3>注册</h3>
                <span class="before-input">用户名:</span> <input name="registerName" /> <br/>
                <span class="before-input">密 码:</span> <input type="password" name="registerPass" /> <br/>
                <button onclick="register()">注册</button>
            </div>
        </div>

    </div>

    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script>
        async function login(){
            try{
                const name = $('input[name="loginName"]').val()
                const pass = $('input[name="loginPass"]').val()
                if(isValid(name,pass)){
                    afterLogin(await ajaxLogin(name,pass))
                }else{
                    alert('用户名和密码必须在8到16位之间且开头不能为数字')
                }
            }catch(e){
                alert("登录发生错误:" + e)
            }
        }

        function afterLogin({ok,msg}){
            if(ok){
                showSuccess()
                redirectGamePage()
            }else{
                showFail(msg)
            }
        }

        function redirectGamePage(){
            window.location.replace('/game.html')
        }

        function showSuccess(){
            alert('登录成功，跳转到游戏页面')
        }

        function showFail(msg){
            alert('登录失败，出错信息: '+msg)
        }

        function ajaxLogin(name,pass){
            return new Promise((resolve,reject) => {
                $.ajax({
                    url: '/user/login',
                    data: {name,pass},
                    method: 'POST',
                }).then(res => {
                    resolve(res)
                },err => {
                    reject('服务器响应错误')
                })
            })
        }

        async function register(){
            try{
                const name = $('input[name="registerName"]').val()
                const pass = $('input[name="registerPass"]').val()
                if(isValid(name,pass)){
                    afterRegisterd(await ajaxRegister(name,pass))
                }else{
                    alert('用户名和密码必须在8到16位之间且开头不能为数字')
                }
            }catch(e){
                alert("注册发生错误:" + e)
            }
        }

        function isValid(name,pass){
            const regObj = /[a-zA-Z]\w{7,15}/

            return (regObj.test(name) && regObj.test(pass))
        }
        
        function afterRegisterd({ok,msg}){
            if(!ok){
                alert("注册错误: "+msg)
                return
            }
            alert('注册成功')
        }

        function ajaxRegister(name,pass){
            return new Promise((resolve,reject) => {
                $.ajax({
                    url: '/user/register',
                    method: 'POST',
                    //contentType: "application/json; charset=utf-8",
                    data: {name,pass},
                }).then(res => {
                    resolve(res)
                },err => {
                    reject("服务器响应出错")
                })
            }) 
        }
    </script>
</body>

</html>